package servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Key;


import dataTypes.*;

/*
 * A user choose a deal from the main list to view more details and possibly sigh.
 */
public class showCouponDetails extends HttpServlet {

	public showCouponDetails() {
		super();
	}

	@SuppressWarnings("deprecation")
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			response.setContentType("text/html");
			PrintWriter pw = response.getWriter();
						
			
			int bId = Integer.parseInt(request.getParameter("bId"));
			int dId = Integer.parseInt(request.getParameter("dId"));
		  
			//<li><p class="normal"><strong>Finish Demo</strong></p></li>
						
			Key bKey=KeyFactory.createKey(business.class.getSimpleName(),bId);
			Key dKey=KeyFactory.createKey(bKey,deal.class.getSimpleName(),dId);

			PersistenceManager pm=PMF.get().getPersistenceManager();

			
			deal d=(deal)pm.getObjectById(deal.class,dKey);
			
			pw.println("<ul class='dealinfoLIST'>");
			pw.println("<li class = 'dealinfoLIST' style='height:150px;'><a href ='" + d.getBusiness().getSiteLink()+ "' rel='external'> <img src='" + d.getBusiness().getPhotoLink() + "' height='140' width='70%'/></a>  </li> ");
			pw.println("<li class = 'dealinfoLIST' style='text-align:left; color:#000'>" + d.getBusiness().getDescription() + "</li>");
			pw.println("<li class = 'dealinfoLIST' style='text-align:left;color:#000'> And today we have " + d.getPercent() + "  Percent DISCOUNT" + "</li>");
			pw.println("<li class = 'dealinfoLIST' style='text-align:left;color:#000'> Only " + d.getAmount() + "  Coupons" + "<BR> valid until: " + d.getEnd().getHours() + ":" + d.getEnd().getMinutes() + "</li>");
			pw.println("<li class = 'dealinfoLIST' style='height:50px; margin-top:36px; text-align:left; color:#000'><a href='/useraddcoupon?bId=" +
					d.getBusiness().getKey().getId() + "&dId="  + d.getKey().getId() + "&uId=2" + /*d. + */  "&num=1" + /*d.get + */  "' rel='external'class='whiteButton submit'>Take DEAL!</a></li>");
			pw.println("</ul>");
			
			
			//pw.println("<img src='http://www.google.co.il/logos/2011/icecreamsundae11-ig.jpg' height='80' width='80' style='float:left;'/>");
			
			// received query from client. 
			// to retrieve full info about the deal and business to display
		
			/* business and deal DB access here */
			
			
			
		} catch (Exception e) { 
			e.printStackTrace();
			
		}		

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}